import { WeaSearchGroup,WeaSelect,WeaTools ,WeaErrorPage ,WeaFormItem ,WeaBrowser,WeaDialog,WeaRightMenu,WeaCheckbox} from 'ecCom';
import { WeaSwitch } from 'comsMobx';
import {inject, observer} from "mobx-react/index";
import { Button , Modal} from 'antd';
import {WeaLocaleProvider} from 'ecCom';
import * as mobx from 'mobx';
import isEmpty from 'lodash/isEmpty';
import {checkEditRight} from "./workflowFreeUtils";

const { toJS } = mobx;
const getLabel = WeaLocaleProvider.getLabel;

@observer
class FreeNodeReturnBackSetModal extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
        }
    }
    render(){
        const { workflowNewDesignStore } =  this.props;
        const { openReturnBackSetting , returnBackDialogShow} = workflowNewDesignStore;
        return (
            <div>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@oryxs6`}
                    icon = {'icon-coms-workflow'}
                    iconBgcolor="#0079DE"
                    onCancel={()=>{openReturnBackSetting(false,true)}}
                    title={getLabel(84508,'退回设置')}
                    visible={returnBackDialogShow}
                    style={{width: '600px', height: '180px'}}
                    hasScroll
                    closable={true}
                    buttons={this.getButtons()}
                    moreBtn={{datas:this.getRightMenu(),collectParams:{favname:getLabel(84508,'退回设置'),favouritetype: '2', importantlevel: 1}}}
                >
                    <div className="wfFree-flowSet-returnBack-setting">
                        {this.getSearchs()}
                    </div>
                </WeaDialog>
            </div>
        )
    }
    // weaForm 自定义渲染
    getSearchs = () =>  {
        const { workflowNewDesignStore } =  this.props;
        const { returnBackConditions ,  returnBackForm , currentChoosedItemId , getFreeObjById , wfFreeDatas,
        tempReturnBackValue,currentChoosedItemDataIndex,currentPostParams} = workflowNewDesignStore;

        /** 获取当前 准确位置上的自由节点数据*/
        let startId =  currentChoosedItemDataIndex.split('_')[0];
        let datasIndex = '' , currentFreeDatas = {};
        if(startId != '' && !isEmpty(toJS(wfFreeDatas)) && wfFreeDatas[startId]){
            datasIndex =  currentChoosedItemDataIndex.split('_')[1];
            currentFreeDatas = wfFreeDatas[startId][datasIndex];
        }
        /************/

        let currentElement = getFreeObjById(currentChoosedItemId,currentFreeDatas) || {};
        const { nodeEntity = {}} = currentElement;
        const { nodeStatus = '' , nodetype = ''} = nodeEntity;
        const { isrejectremind = '0' , ischangrejectnode = '0' } = tempReturnBackValue;
        const {isFormInit} = returnBackForm;
        const formParams = returnBackForm.getFormParams();
        let group = [] , rejectComs = [];

        let isReadOnly = checkEditRight(currentElement,currentPostParams);

        isFormInit && toJS(returnBackConditions).map((c,ecindex) =>{
            let items = [];
            c.items.map(field => {
            let col = 1;

            const _domkey = field.domkey[0];
            field.viewAttr = isReadOnly ? 1 : field.viewAttr;
            if(_domkey == 'ischangrejectnode' || _domkey == 'isrejectremind'){
                if(_domkey == 'ischangrejectnode'){
                    rejectComs.push(
                        <WeaCheckbox ecId={`${this && this.props && this.props.ecId || ''}_WeaCheckbox@oh4mmh@${_domkey}`}
                            viewAttr = {isReadOnly?1:2}
                            value={ischangrejectnode+''}
                            onChange={(value) => {
                                tempReturnBackValue['ischangrejectnode'] = value;
                            }}
                            content={field.label}
                        />
                    )
                }else{
                    field.value = isrejectremind;
                    items.push({
                        com:(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@cpyi44@${_domkey}`}
                            label={`${field.label}`}
                            labelCol={{span: `${field.labelcol}`}}
                            ratio1to2
                            error={returnBackForm.getError(field)}
                            tipPosition="bottom"
                            wrapperCol={{span: `${field.fieldcol}`}}>
                                <div>
                                    <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@t2bbga@${_domkey}`} fieldConfig={field} form={returnBackForm} onChange={this.onChange}/>
                                    {isrejectremind == '1' && rejectComs}
                                </div>
                            </WeaFormItem>),
                        col:col,
                        hide: returnBackForm.isHide(field, (keys, allParams)=> {
                        return false;
                        })
                    }) 
                }
            }else{
                items.push({
                    com:(<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@mvyev6@${_domkey}`}
                        label={`${field.label}`}
                        labelCol={{span: `${field.labelcol}`}}
                        ratio1to2
                        error={returnBackForm.getError(field)}
                        tipPosition="bottom"
                        wrapperCol={{span: `${field.fieldcol}`}}>
                        <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@ztbcjo@${_domkey}`} fieldConfig={field} form={returnBackForm} onChange={this.onChange}/>
                        </WeaFormItem>),
                    col:col,
                    hide: returnBackForm.isHide(field, (keys, allParams)=> {
                    return false;
                    })
                })
            }
            
            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@75yy4d@${ecindex}`} needTigger={false} title={c.title} showGroup={c.defaultshow || true} items={items}/>)
        });
        return group;
    }
    onChange = (v) => {
        const { workflowNewDesignStore } =  this.props;
        const { currentChoosedItemId , getFreeObjById , wfFreeDatas , tempReturnBackValue,currentChoosedItemDataIndex} = workflowNewDesignStore;
        /** 获取当前 准确位置上的自由节点数据*/
        let startId =  currentChoosedItemDataIndex.split('_')[0];
        let datasIndex = '' , currentFreeDatas = {};
        if(startId != '' && !isEmpty(toJS(wfFreeDatas)) && wfFreeDatas[startId]){
            datasIndex =  currentChoosedItemDataIndex.split('_')[1];
            currentFreeDatas = wfFreeDatas[startId][datasIndex];
        }
        /************/
        let currentElement = getFreeObjById(currentChoosedItemId,currentFreeDatas) || {};
        const { nodeEntity = {}} = currentElement;

        for(let key in v){
            workflowNewDesignStore.tempReturnBackValue[key] = v[key].value+'';
        }
    }
    dataSave = () => {
        const { workflowNewDesignStore } =  this.props;
        const {openReturnBackSetting,currentChoosedItemId,wfFreeDatas,getFreeObjById,tempReturnBackValue,currentChoosedItemDataIndex} = workflowNewDesignStore;
        /** 获取当前 准确位置上的自由节点数据*/
        let startId =  currentChoosedItemDataIndex.split('_')[0];
        let datasIndex = '' , currentFreeDatas = {};
        if(startId != '' && !isEmpty(toJS(wfFreeDatas))  && wfFreeDatas[startId]){
            datasIndex =  currentChoosedItemDataIndex.split('_')[1];
            currentFreeDatas = wfFreeDatas[startId][datasIndex];
        }
        /************/
        let currentElement = getFreeObjById(currentChoosedItemId,currentFreeDatas) || {};
        let { nodeEntity = {}} = currentElement;
        // nodeEntity = {...nodeEntity,...tempReturnBackValue};
        currentElement.nodeEntity = {...nodeEntity,...tempReturnBackValue};
        openReturnBackSetting(false);
    }

    getRightMenu = () => {
        const { workflowNewDesignStore } =  this.props;
        const {openReturnBackSetting,currentChoosedItemId,wfFreeDatas,getFreeObjById,currentChoosedItemDataIndex,currentPostParams} = workflowNewDesignStore;
        /** 获取当前 准确位置上的自由节点数据*/
        let startId =  currentChoosedItemDataIndex.split('_')[0];
        let datasIndex = '' , currentFreeDatas = {};
        if(startId != '' && !isEmpty(toJS(wfFreeDatas))  && wfFreeDatas[startId]){
            datasIndex =  currentChoosedItemDataIndex.split('_')[1];
            currentFreeDatas = wfFreeDatas[startId][datasIndex];
        }
        /************/
        let currentElement = getFreeObjById(currentChoosedItemId,currentFreeDatas) || {};
        const { nodeEntity = {}} = currentElement;
        const { nodeStatus = '0' , nodetype = ''} = nodeEntity;
        let isReadOnly = checkEditRight(currentElement,currentPostParams);

        let btns = [];
        if(!isReadOnly){
            btns.push({
                key:"save",
                icon: <i className='icon-coms-Preservation'/>,
                content:getLabel(86,'保存'),
                onClick: key =>{
                    // openReturnBackSetting(false);
                    this.dataSave();
                }
            });
        }
        return btns;
    }
    getButtons = () => {
        const { workflowNewDesignStore } =  this.props;
        const {openReturnBackSetting,currentChoosedItemId,wfFreeDatas,getFreeObjById,currentChoosedItemDataIndex,currentPostParams} = workflowNewDesignStore;
        /** 获取当前 准确位置上的自由节点数据*/
        let startId =  currentChoosedItemDataIndex.split('_')[0];
        let datasIndex = '' , currentFreeDatas = {};
        if(startId != '' && !isEmpty(toJS(wfFreeDatas))  && wfFreeDatas[startId]){
            datasIndex =  currentChoosedItemDataIndex.split('_')[1];
            currentFreeDatas = wfFreeDatas[startId][datasIndex];
        }
        /************/
        let currentElement = getFreeObjById(currentChoosedItemId,currentFreeDatas) || {};
        const { nodeEntity = {}} = currentElement;
        const { nodeStatus = '' , nodetype = ''} = nodeEntity;

        let isReadOnly = checkEditRight(currentElement,currentPostParams);
        let btns = [];

        if(!isReadOnly){
            btns.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@scnpuw@save`} type="primary"  onClick={()=>{
                    // openReturnBackSetting(false)
                    this.dataSave();
                }}>{getLabel(86,'保存')}</Button>
            );
        }
        return btns;
    }
    // dataSave = () => {
    //
    // }
}

export default WeaTools.tryCatch(React, 
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@hu64b1`} msg={ props.error ? props.error : '对不起，该页面异常，请联系管理员！'} />,
    {error: ""}
)(FreeNodeReturnBackSetModal);